using System;
using System.Collections.Generic;
using System.Text;
using SpoonCore.Tools;

namespace SpoonCore.Storage
{
	class StorageClient
	{
		private readonly Random _rand = new Random();
		private readonly StorageManager _manager = new StorageManager(@"F:\spoonnet\storage");
		private SortedList<int, LookupWaiter<AbstractStorageItem>> _waiterList = new SortedList<int, LookupWaiter<AbstractStorageItem>>();

		public T Get<T>(SpoonIdentifier id) where T : AbstractStorageItem, new()
		{
			Storage<T> storage = _manager.GetStoreage<T>();
			T res = storage[id];
			if (res != null) return null;
			else
			{
				int transId = _rand.Next();
				LookupWaiter<AbstractStorageItem> waiter = new LookupWaiter<AbstractStorageItem>();
				_waiterList.Add(transId, waiter);

				//send message

				waiter.ResetEvent.WaitOne();
				if (waiter.Result != null) res = (T) waiter.Result;
			}
			return res;
		}

		public void Set(AbstractStorageItem item)
		{

		}
	}
}
